<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :span="6">
              <a-form-item label="小程序">
                <a-select placeholder="请选择" v-model="queryParam.mini_id" @change="getList()">
                  <a-select-option :value="item.id" v-for="item of idList" :key="item.id">
                    {{ item.app_name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>

            <a-col :span="6">
              <a-form-item label="">
                <a-button type="primary" @click="getList()">查询</a-button>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>

    <a-card :border="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <div>
          <a-radio-group v-model="queryParam.status" @change="getList()">
            <a-radio-button :value="-1"> 全部 </a-radio-button>
            <a-radio-button :value="0"> 审核中 </a-radio-button>
            <a-radio-button :value="1"> 生效中 </a-radio-button>
            <a-radio-button :value="2"> 已失效 </a-radio-button>
            <a-radio-button :value="3"> 审核拒绝 </a-radio-button>
          </a-radio-group>
        </div>

        <a-button type="primary" @click="$router.push({ name: 'coupoPromotionEdit' })" icon="plus">新建</a-button>
      </div>

      <a-table
        size="default"
        :columns="columns"
        :data-source="list"
        :row-key="record => record.id"
        :pagination="{
          total: this.total,
          pageSize: this.pageSize,
          showSizeChanger: false,
          onChange: this.tablePageChange
        }"
      >
        <span slot="time" slot-scope="text, record">
          {{ record.delivery_begin_time }}至{{ record.delivery_end_time }}
        </span>

        <span slot="activity_name" slot-scope="text, record">
          <a-tooltip>
            <template slot="title">
              {{ text.activity_name }}
            </template>
            <div class="name">
              {{ text.activity_name }}
            </div>
          </a-tooltip>
        </span>

        <span slot="type" slot-scope="text, record">
          {{ text.type === 0 ? '支付有礼' : '满减' }}
        </span>

        <span slot="status" slot-scope="text, record">
          <a-tooltip>
            <template slot="title" v-if="text.fail_reason">
              {{ text.fail_reason }}
            </template>
            <div class="name">
              <a-badge :color="getStatus(text.status)[1]" :text="getStatus(record.status)[0]" />
            </div>
          </a-tooltip>
        </span>

        <span slot="action" slot-scope="text, record">
          <a-button type="link" @click="handle('stop', text)" :disabled="text.status !== 1">
            禁用
          </a-button>
        </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { activityDeliveryStop, activityDeliverylist } from '@/api/marketing/alipayPromotion'
import { alipayList } from '@/api/setting/applet'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 100
  },
  {
    title: '名称',
    scopedSlots: {
      customRender: 'activity_name'
    }
  },
  {
    title: '最低消费',
    dataIndex: 'delivery_floor_amount'
  },
  {
    title: '发放方式',
    scopedSlots: {
      customRender: 'type'
    }
  },
  {
    title: '优惠金额',
    dataIndex: 'sub_price'
  },
  {
    title: '领取数量',
    dataIndex: 'count',
    sorter: (a, b) => parseInt(a.count) - parseInt(b.count)
  },
  {
    title: '使用数量',
    dataIndex: 'use_count',
    sorter: (a, b) => parseInt(a.use_count) - parseInt(b.use_count)
  },

  {
    title: '状态',
    scopedSlots: {
      customRender: 'status'
    }
  },
  {
    title: '活动有效期',
    scopedSlots: {
      customRender: 'time'
    },
    width: 200
  },
  {
    title: '创建时间',
    dataIndex: 'created_at'
  },
  {
    title: '操作',
    scopedSlots: {
      customRender: 'action'
    },
    width: 150
  }
]

export default {
  data() {
    return {
      total: 0,
      pageNo: 1,
      pageSize: 10,
      columns,
      idList: [],
      queryParam: {
        mini_id: '',
        status: -1,
        delivery_booth_code: 2
      },

      list: []
    }
  },

  computed: {
    getStatus() {
      return status => {
        let text, color
        switch (parseInt(status)) {
          case 0:
            text = '审核中'
            color = '#1890ff'
            break

          case 1:
            text = '生效中'
            color = 'rgb(101, 200, 23)'
            break

          case 2:
            text = '已失效'
            color = '#FF4D4F'
            break

          case 3:
            text = '审核拒绝'
            color = '#FF4D4F'
            break
        }
        return [text, color]
      }
    }
  },

  mounted() {
    const hide = this.$message.loading('加载中', 0)

    alipayList()
      .then(res => {
        hide()
        if (!res.code && res.data && res.data.length) {
          this.idList = res.data
          this.queryParam.mini_id = res.data[2].id
          this.getList()
        }
      })
      .catch(err => {
        hide()
      })
  },

  methods: {
    handle(type, { id }) {
      const hide = this.$message.loading('加载中', 0)

      switch (type) {
        case 'stop':
          activityDeliveryStop(id)
            .then(res => {
              hide()
              if (!res.code) {
                this.$message.success(res.msg)
                this.getList()
                return
              }

              this.$message.error(res.msg)
            })
            .catch(() => {
              hide()
            })
          break
      }
    },

    // 获取数据列表
    async getList(page = 1) {
      const hide = this.$message.loading('加载中', 0)
      let { queryParam, pageSize } = this

      const res = await activityDeliverylist({
        ...queryParam,
        pageSize: String(pageSize),
        page: String(page)
      })

      if (res.code == 0) {
        let { totalCount: total, data: list } = res.data
        console.log('list,', list)
        this.total = total
        this.list = list || []
      }
      hide()
    },

    // 分页请求
    tablePageChange(e) {
      this.getList(e)
    },

    // 点击单个
    handleItem(type, { id }) {
      switch (type) {
        case 'stop':
          activityDeliveryStop(id).then(res => {
            if (!res.code) {
              this.$message.success(res.msg)
              this.getList()
              return
            }

            this.$message.error(res.msg)
          })
          break
      }
    }
  }
}
</script>

<style lang="less" scoped>
.name {
  width: 160px;
}
</style>
